package com.leon.androidcoursedesign.utils.dataBaseUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import androidx.annotation.Nullable;

import com.leon.androidcoursedesign.adapters.baseAdapter.BaseAdapter;
import com.leon.androidcoursedesign.beans.Course;

import java.util.ArrayList;
import java.util.List;

public class CourseUtils extends SQLiteHelper {
    private final SQLiteHelper sqLiteHelper;
    private final SQLiteDatabase sqLiteDatabase;
    private final String dbName = Tables.Course.value;

    public CourseUtils(@Nullable Context context) {
        super(context, 1);

        this.sqLiteHelper = new SQLiteHelper(context, 1);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
    }

    /**
     * 查询所有未删除的课程
     *
     * @return  返回所有课程
     */
    public List<Course> queryAllCourse(String operationId) {
        if (operationId != null) {
            Cursor cursor = sqLiteDatabase.query(dbName, null, "isDelete = ? AND operationId = ?", new String[]{"false", operationId}, null, null, null);

            List<Course> courses = new ArrayList<>();
            while (cursor.moveToNext()) {
                Course course = new Course();
                course.id = cursor.getInt(cursor.getColumnIndex("id"));
                course.itemId = cursor.getString(cursor.getColumnIndex("itemId"));
                course.operationId = cursor.getString(cursor.getColumnIndex("operationId"));
                course.courseName = cursor.getString(cursor.getColumnIndex("courseName"));
                course.teacher = cursor.getString(cursor.getColumnIndex("teacher"));
                course.location = cursor.getString(cursor.getColumnIndex("location"));
                course.period = cursor.getString(cursor.getColumnIndex("period"));
                course.credit = cursor.getString(cursor.getColumnIndex("credit"));

                courses.add(course);
            }
            cursor.close();
            return courses;
        }

        return null;
    }

    /**
     * 将课程添加至‘Course’
     *
     * @param course    course对象
     * @return  返回插入状态
     */
    public boolean addCourse(Course course) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("itemId", course.itemId);
        contentValues.put("operationId", course.operationId);
        contentValues.put("courseName", course.courseName);
        contentValues.put("teacher", course.teacher);
        contentValues.put("location", course.location);
        contentValues.put("period", course.period);
        contentValues.put("credit", course.credit);

        long insert = sqLiteDatabase.insert(dbName, null, contentValues);

        return insert > 0;
    }

    /**
     * 检查course是否已存在
     *
     * @param courseName    course name
     * @return  返回存在状态
     */
    public boolean checkCourse(String operationId, String courseName) {
        Cursor cursor = sqLiteDatabase.query(dbName, null, "isDelete = ? AND courseName = ? AND operationId = ?", new String[]{"false",courseName, operationId}, null, null, null);
        int count = cursor.getCount();
        cursor.close();

        return count > 0;
    }

    /**
     * 根据course name删除对应course
     *
     * @param itemId    itemId
     * @return  返回删除状态
     */
    public boolean deleteCourse(String itemId) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("isDelete", true);

        int state = sqLiteDatabase.update(dbName, contentValues, "itemId = ?", new String[]{itemId});

        return  state > 0;
    }

    /**
     * 更新指定course的数据
     *
     * @param course    新的course数据
     * @return  返回存在状态
     */
    public boolean updateCourse(Course course) {
        Log.d("Leon", "updateCourse: " + course.operationId);
        ContentValues contentValues = new ContentValues();
        contentValues.put("courseName", course.courseName);
        contentValues.put("teacher", course.teacher);
        contentValues.put("location", course.location);
        contentValues.put("period", course.period);
        contentValues.put("credit", course.credit);

        Log.d("Leon", "updateCourse: " + course.toString());

        int state = sqLiteDatabase.update(dbName, contentValues, "itemId = ? AND operationId = ?", new String[]{course.itemId, course.operationId});

        return  state > 0;
    }
}
